package code.Array;

import java.util.Arrays;
import java.util.HashMap;

public class SetMismatch {
    public static void main(String[] args) {
        int[] array = {2, 2};
        System.out.println("Output:" + Arrays.toString(findErrorNums1(array)));
    }

    public static int[] findErrorNums(int[] nums) {
        int[] result = new int[2];
        HashMap<Integer, Boolean> arr = new HashMap<>();
        for (int i = 1; i <= nums.length; i++) {
            arr.put(i, false);
        }
        for (int i = 1; i <= nums.length; i++) {
            if (arr.get(nums[i - 1])) {
                result[0] = nums[i - 1];
                //result[1] = i;
            } else {
                arr.put(nums[i - 1], true);
            }
        }
        for (int i = 1; i <= nums.length; i++) {
            if (!arr.get(i)) {
                result[1] = i;
            }
        }
        return result;
    }

    public static int[] findErrorNums1(int[] nums) {
        HashMap<Integer, Integer> map = new HashMap<>();
        int dup = -1;
        int missing = 1;
        for (int num : nums) {
            map.put(num, map.getOrDefault(num, 0) + 1);
        }
        for (int i = 1; i <= nums.length; i++) {
            if (map.containsKey(i)) {
                if (map.get(i) == 2) {
                    dup = i;
                }
            } else {
                missing = i;
            }
        }
        return new int[]{dup, missing};
    }
}
